package aimis.controller;


import jakarta.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import aimis.constant.ResponseEnum;
import aimis.domain.Attachment;
import aimis.dto.Result;
import aimis.repo.AttachmentRepo;
import aimis.util.DownloadUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
public class AttachmentController {

    @Autowired
    AttachmentRepo attachmentRepo;
    @Value("${upload.tmp}")
    private String tmp;
    @Value("${upload.files}")
    private String filePath;

//    @PostMapping(value = "/attachment/save")
//    public Result save(MultipartFile file) throws IOException {
//        String originalFilename = file.getOriginalFilename();
//        String[] filename = originalFilename.split("\\.");
//        String path=new File("").getCanonicalPath()+"/"+UUID.randomUUID()+'.'+filename[1];
//        File dst= new File( new File("").getCanonicalPath()+"/"+UUID.randomUUID()+'.'+filename[1]);
//        file.transferTo(dst);
//
//        Attachment attachment = new Attachment();
//        attachment.setPath(path);
//        attachment.setFileName(originalFilename);
//        attachment.setMineType(file.getContentType());
//        attachmentRepo.save(attachment);
//        return Result.success("success");
//    }

    @PostMapping(value = "/attachment/upload")
    public Result upload(MultipartFile file) throws IOException {
        //并发量不大时用此方法没有问题
        String tmpFileName = UUID.randomUUID().toString();
        FileOutputStream output = new FileOutputStream(new File(tmp, tmpFileName));
        IOUtils.copy(file.getInputStream(), output);
        return Result.success(ResponseEnum.OK).add("attachment", new Attachment(file.getOriginalFilename(), tmpFileName, file.getContentType()));
    }

    @GetMapping(value = "/attachment/{tmpName}")
    public void download(@PathVariable String tmpName, String name, String mineType, HttpServletResponse response) throws IOException {
        File file = new File(tmp, tmpName);
        if (file.exists()) {
            if (mineType != null && mineType.contains("image")) {
                DownloadUtils.preview(file, mineType, name, response);
            } else {
                DownloadUtils.download(file, name, response);
            }
        } else {
            if (mineType != null && mineType.contains("image")) {
                DownloadUtils.preview(new File(filePath, tmpName), mineType, name, response);
            } else {
                DownloadUtils.download(new File(filePath, tmpName), name, response);
            }
        }
    }

    @GetMapping(value = "/attachment/image/{tmpName}")
    public void image_preview(@PathVariable String tmpName, String name, HttpServletResponse response) throws IOException {
        File file = new File(tmp, tmpName);
        var attachment = attachmentRepo.findByPath(tmpName);
        if (name == null || "".equals(name)) {
            name = attachment.getFileName();
        }
        if (file.exists()) {
            DownloadUtils.preview(file, attachment.getMineType(), name, response);
        } else {
            DownloadUtils.preview(new File(filePath, tmpName), attachment.getMineType(), name, response);
        }
    }

}
